<?php
namespace Home\Controller;
use Think\Controller;

/******************会议申请控制器********************/

class IndexController extends CommonController {
    public function __construct(){
        parent::__construct();
        //测试数据
        //$this->userid = 'WangJun';
    }

    /*
     * 本周会议  默认当天的会议室使用情况
     */
    public function this_week(){
        //获取时间
        $date = I('get.date');
        if(!$date){
            $use_date = date('Y-m-d');
            $weekarray=array("日","一","二","三","四","五","六");

            $date = $use_date.' 星期'.$weekarray[date('w')];
        }else{
            $use_date = explode('--',$date)[0];
            $date = str_replace('--',' ',$date);
        }
        //dump($date);
        //获取会议室使用情况
        $meeting_syqk = D('meeting_room');
        $data = $meeting_syqk->get_meeting_syqk_info($use_date);

        $this->assign(array(
            'data' => $data,
            'date' => $date
        ));
        //dump($data);exit;
        $this -> display();
    }


    //会议室使用情况
    public function meeting_syqk(){
        $date = I('get.date');
        $meeting_room_id = I('get.meeting_room');
        $sjd = I('get.sjd');
        $syqk = I('get.syqk');

        //取出当前会议室当天当前时间段的使用情况
        $meeting_syqk = D('meeting_syqk');
        $yi_syqk = $meeting_syqk->get_meeting_syqk_info($date,$meeting_room_id,$sjd);

        /********************************算出未占用的时间***********************************/
        $wei_syqk = [];  //未占用的时间段
        if($syqk != '已满'){
            //获取当前会议室的数据
            $room_info = D('meeting_room')->where("id={$meeting_room_id}")->find();
            $use_time = explode(';',$room_info['use_time']);
            //计算出当前会议室当前时间段的开始和结束时间
            $_v = [];
            foreach($use_time as $K=>$v){
                if(strpos($v,$sjd) !== false){
                    $_v = explode('/',$v);
                    $_v = explode('--',$_v[1]);
                }
            }
            $start_time = strtotime($_v[0]);  //会议室可使用数据开始
            $end_time = strtotime($_v[1]);    //会议室可使用数据结束
            //dump($_v[0]);dump($_v[1]);exit;

            //计算会议室的未使用时间段
            $wei_syqk = $this->get_meeting_room_weisyqk($yi_syqk,$start_time,$end_time);
            //dump($wei_syqk);exit;
        }
        /************************************取出占用详细情况*****************************/
        $use_syqk = [];  //已占用的详细情况
        if($yi_syqk){
            $use_syqk = D('meeting_notes')->getNotesUseSyqk($yi_syqk);
        }
        //dump($use_syqk);exit;

        $this->assign(array(
            'id'       => $meeting_room_id,  //会议室ID
            'date'     => $date,    //日期
            'sjd'      => $sjd,    //时间段
            'use_syqk'  => $use_syqk ,   //已占用的时间段
            'wei_syqk' => $wei_syqk,   //未占用的时间段
        ));
        $this -> display();
    }

    /*
     * 计算会议室的未使用时间段
     */
    public function get_meeting_room_weisyqk($data,$start_time,$end_time){
        $wei_syqk = [];
        if($data){
            /*********************如果有占用数据,计算出未占用的时间段****************/
            $date_time = array_column($data,'date_time');  //已占用的时间段
            //dump($date_time);
            //把占用的时间转换成数组并按高低排序
            $time = explode('--',implode('--',$date_time));
            //dump($time);exit;
            $time_info = [];
            foreach($time as $k=>$v){
                $time_info[] = strtotime($v);
            }
            sort($time_info);

            //首先算出首尾空闲时间段,去除小于1800秒即30分钟的时间段
            $shou_time = $wei_time = '';  //首尾时间段
            if( ($time_info[0] != $start_time) && ($time_info[0]-$start_time >= 1800) ){
                $shou_time = date('H:i',$start_time).'--'.date('H:i',$time_info[0]);
            }
            if( (end($time_info) != $end_time) && ($end_time-end($time_info) >= 1800) ){
                $wei_time = date('H:i',end($time_info)).'--'.date('H:i',$end_time);
            }
            //dump($shou_time);
            //dump($wei_time);exit;
            //拼接出已经占用的时间段和中间空余的时间段 去除小于1800秒即30分钟的时间段 例如 9--10,10--10.30,10.30--11.30
            $keys = array_keys($time_info);
            $end_keys = end($keys);
            foreach($time_info as $k=>$v){
                if($k < $end_keys){
                    if( ($time_info[$k] != $time_info[$k+1]) && ($time_info[$k+1]-$time_info[$k] >= 1800) ){
                        $wei_syqk[] = date('H:i',$time_info[$k]).'--'.date('H:i',$time_info[$k+1]);
                    }
                }
            }
            //dump($wei_syqk);
            //把首尾时间段加入到未占用时间段
            if($shou_time){
                array_unshift($wei_syqk,$shou_time);
            }
            if($wei_time){
                array_push($wei_syqk,$wei_time);
            }
            //dump($wei_syqk);
            //在未占用时间段里去除已经占用的时间段
            foreach($wei_syqk as $k=>$v){
                if(in_array($v,$date_time)){
                    unset($wei_syqk[$k]);
                }
            }
        }else{
            /*********************如果没有数据,未占用时间段为此会议室的当前时间段************/
            $wei_syqk[] = date('H:i',$start_time).'--'.date('H:i',$end_time);
        }

        return $wei_syqk;
    }

    //预约会议
    public function reserve(){
        $user_info = $this->getUserList($this->userid);
        if(!IS_POST){
            //jsdk接口验证
            $this->jsdk_conf();
            //通讯录选人验证
            $this->openEnterpriseContact();
            $get_info = I('get.');
            //获取当前会议室和当前时间数据
            $meeting_room_name = D('meeting_room')->field('name')->find($get_info['id']);
            $use_time = explode('--',$get_info['use_time']);
            //获取全部成员数据
            $user_all_info = $this->cache_all_user();
            //获取群组数据
            $group = D('group_member');
            $group_info = $group->getGroupInfo();

            $this->assign(array(
                'room_id' => $get_info['id'],
                'sjd' => $get_info['sjd'],
                'room_name' => $meeting_room_name['name'],
                'date' => $get_info['date'],
                'use_time' => $use_time,
                'user_info' => $user_info,
                'user_all_info' => json_encode($user_all_info),
                'group' => $group_info,
            ));
            $this->display();
        }else{
            $data = I('post.');
            $start_time = explode(' ',$data['start_time']);
            $end_time = explode(' ',$data['end_time']);
            $use_time = $start_time[1].'--'.$end_time[1];
            //dump($use_time);exit;
            //开启事物
            M()->startTrans();
            if($data['stauts'] == 'add'){
                $type = 1;
                $msg = '提交成功';
                //添加会议室使用情况
                $syqkid = D('meeting_syqk')->add(array(
                    'meeting_room_id' => $data['room_id'],
                    'date' => $start_time[0],
                    'date_modle' => $data['sjd'],
                    'date_time' => $use_time
                ));
                if(!$syqkid){
                    M()->rollback();  //事物回滚
                }
            }else{
                $type = 2;
                $msg = '保存成功';
            }
            //添加会议记录
            $notesid = D('meeting_notes')->add(array(
                'meeting_room_id' => $data['room_id'],
                'date' => $start_time[0],
                'use_time' => $use_time,
                'theme' => $data['theme'],
                'subject' => $data['subject'],
                'host_name' => $data['host_name'],
                'ch_userids' => $data['ch_userids'],
                'ch_bumens' => $data['ch_bumens'],
                'ch_qz' => $data['groupid'] ? implode(',',$data['groupid']) : '',
                'wai_users' => $data['wai_users'],
                'intro' => $data['intro'],
                'apply' => $this->userid,
                'apply_name' => $user_info['user_name'],
                'cb_bumen' => $data['cb_bumen'],
                'tel' => $data['tel'],
                'warm' => $data['warm'] ? implode(',',$data['warm']) : '',
                'type' => $type
            ));
            if($notesid){
                M()->commit(); //提交事物
                $this->success($msg,U('this_week'),3);
            }else{
                M()->rollback();   //事物回滚
                $this->error('操作失败');
            }
        }

    }


    public function ajax_jcqz(){
        $qzid = I('get.qzid');
        $time = I('get.time');
        $data = D('group_member')->jcQzInfo($qzid,$time);
        $this->ajaxReturn($data);
    }

    //下周会议
    public function next_week(){
        $this -> display();
    }

    //我的申请
	public function apply(){
		$this -> display();
	}

	//公司会表
	public function company(){
		$this -> display();
	}

	public function info(){
		$this -> display();
	}

	public function infos(){
		$this -> display();
	}
    //我的会议
	public function my_meeting(){
		$this -> display();
	}





    //生成网页授权URL
	public function authurl(){
	    if(!IS_POST){
	        $this->display();
        }else{
            $corpid = C('CORPID');
            $app_url = C('APP_URL');
            $p = I('post.modle');
            $c = I('post.contro');
            $a = I('post.ff');
            $auth_url = $app_url.ucfirst($p).'/'.ucfirst($c).'/'.$a;
            $redirect_uri = urlencode($auth_url);
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$corpid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&agentid=126&state={$corpid}#wechat_redirect";
            echo $url;
        }
    }


}